<?php

class User_Controller_Browse_Ajax_Friend_Button extends Nuo_Controller_Resolver
{
	/**
	 * unfriend frequest
	 */
	function doUnfriend($request)
	{
		
	}
	
	
	
	function doAdduser($request)
	{
		$listId = $request -> getParam('list_id');
		$userId = $request -> getParam('user_id');
		
		$user = Nuo::getItem('user',$userId);
		
		$memberId = $user->getItemId();

		$itemApi = Nuo::getApi('membership');
		
		$viewer = Nuo::getUser();
		
		// check quota: number of user within specific list

		$table2 = Nuo::getTable('membership/data');
		
		$list = Nuo::getTable('membership/list')->find($listId)->current();

		$select = $table2 -> select() -> where('list_id=?', $listId) -> where('member_id=?', $memberId);

		$item = $table2 -> fetchRow($select);
		
		if ($item)
		{
			$item -> delete();
			$style = '';
		}
		else
		{
			$item = $table2 -> fetchNew();
			$item -> list_id = $listId;
			$item->member_id   = $memberId;
			$item -> user_id = $userId;
			$item -> created_at = time();
			$item -> save();
			$style = 'active';	
		}

		$html = <<<HTML
<input type="hidden" class="uiHiddenAct" name="act" value="" />
<input type="hidden" name="list_id" value=":list_id" />
<input type="hidden" name="user_id" value=":user_id" />
<a class="uiPosMenuItem" href="" aria-click="jscript" aria-cb="form.submit" aria-act="adduser">
	<span class="uiPosMenuText :style">:title</span>
</a>
<i class="uiHidden uiIconSmallIndicator">&nbsp;</i>
HTML;

		$html = strtr($html, array(
			':title' => $list -> getTitle(),
			':list_id' => $listId,
			':user_id' => $userId,
			':style' => $style
		));

		return  array('html' => $html,'jscript'=>'CardHover.flush()',);
	}

	function doAddlist($request)
	{
		$user = Nuo::getUser();
		$title = $request -> getParam('val');
		
		$itemApi  = Nuo::getApi('membership');
		
		$listObj  = $itemApi->createList($user, array('title'=>$title));

		$userId = $request -> getParam('user_id');

		$html = <<<HTML
<input type="hidden" class="uiHiddenAct" name="act" value="" />
<input type="hidden" name="list_id" value=":list_id" />
<input type="hidden" name="user_id" value=":user_id" />
<a class="uiPosMenuItem" href="" aria-click="jscript" aria-cb="form.submit" aria-act="adduser">
	<span class="uiPosMenuText :style">:title</span>
</a>
<i class="uiHidden uiIconSmallIndicator">&nbsp;</i>
HTML;

		$html = quote_html(strtr($html, array(
			':title' => $listObj -> getTitle(),
			':list_id' => $listObj -> getId(),
			':user_id' => $userId,
			':style' => ''
		)));

		$re = $request -> getParam('re');
		$jscript = "$(\"$re\").before(\"$html\");CardHover.flush();";
		return  array('jscript' => $jscript);
	}

	function doHover($request)
	{
		$id = $request -> getParam('id');

		$user = Nuo::getItem($id);

		$viewer = Nuo::getUser();

		$userApi = Nuo::getApi('user');
		
		$itemApi = Nuo::getApi('membership');

		$isViewer = 0;

		if ($viewer && $viewer -> getId() == $user -> getId())
		{
			$isViewer = 1;
		}

		return  array(
			'html' => Nuo::getView()->render('user/view/controller/browse/ajax/friend/button/hover', array(
				'user' => $user,
				'viewer' => $viewer,
				'isViewer' => $isViewer,
				'checkList' => $itemApi -> getListIdOfMember($viewer, $user),
				'lists' => $itemApi -> getMemberLists($viewer),
				'friendApi' => $userApi,
				'statusId' => $userApi -> getStatusId($viewer, $user),
			)),
			'data_id' => $request -> getParam('data-id'),
		);
	}

	function doRequest($request)
	{
		$itemId = $request -> getParam('id');

		$user = Nuo::getItem($itemId);
		$viewer = Nuo::getUser();

		$api = Nuo::getApi('user');

		$api -> sendFriendRequest($viewer, $user);

		return  array(
			'html' => Nuo::getView()->render('user/view/partial/button/addfriend', array(
				'item' => $user,
				'status' => 2,
				'form' => 0,
			)),
			'html_id' => $request -> getParam('html_id'),
		);
	}

	function doCancel($request)
	{

		$itemId = $request -> getParam('id');
		$user = Nuo::getItem($itemId);
		$viewer = Nuo::getUser();

		$api = Nuo::getApi('user');

		$api -> cancelFriendRequest($viewer,$user);

		return  array(
			'jscript' => 'CardHover.hide();',
			'html' => Nuo::getView()->render('user/view/partial/button/addfriend', array(
				'item' => $user,
				'status' => 0,
				'form' => 0,
			)),
			'html_id' => $request -> getParam('html_id'),
		);
	}

	function doDeny($rquest)
	{
		$itemId = $request -> getParam('id');
		$item = Nuo::getItem($itemId);
		$user = Nuo::getUser();

		$api = Nuo::getApi('user');

		$api -> denyFriendRequest($user, $item);

		return  array(
			'html' => Nuo::getView()->render('friend/view/controller/browse/ajax/request/deny', array(
				'viewer' => $user,
				'user' => $item,
				'form' => 0,
			)),
			'html_id' => $request -> getParam('html_id'),
		);
	}

	function doConfirm($request)
	{
		$itemId = $request -> getParam('id');
		$item = Nuo::getItem($itemId);
		$user = Nuo::getUser();

		$api = Nuo::getApi('user');

		$api -> confirmFriendRequest($user, $item);

		return  array(
			'html' => Nuo::getView()->render('friend/view/controller/browse/ajax/request/confirm', array(
				'viewer' => $user,
				'user' => $item,
				'form' => 0,
			)),
			'html_id' => $request -> getParam('html_id'),
		);
	}

}
